﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TestWizardsForms
{
    public enum Layouts
    {
        Empty = 0,
        TwoPanels,
        FourPanels,
        Formly,
        FormlyTwoPanels,
        FormlyFourPanels,
        FormlyGrid,
        Grid
    }

    public class LayoutsGenerator
    {
        public bool IsGridCRUD { get; set; }
        public string Header1 { get; set; }
        public string GridCRUDActionCode { get; set; }
        public string GridDefinition { get; set; }
        public string ControlLibraryDefinition { get; set; }

        public LayoutsGenerator()
        {
        }

        public string Create(Layouts layout)
        {
            string retValue = string.Empty;

            retValue = CreateInternal(layout);

            return retValue;
        }

        private string CreateInternal(Layouts layout)
        {
            string retValue = string.Empty;

            switch (layout)
            {
                case Layouts.Empty:
                    retValue = CreateEmpty();
                    break;
                case Layouts.TwoPanels:
                    retValue = CreateTwoPanels();
                    break;
                case Layouts.FourPanels:
                    retValue = CreateFourPanels();
                    break;
                case Layouts.Formly:
                    retValue = CreateFormly();
                    break;
                case Layouts.FormlyTwoPanels:
                    retValue = CreateFormlyTwoPanels();
                    break;
                case Layouts.FormlyFourPanels:
                    retValue = CreateFormlyFourPanels();
                    break;
                case Layouts.FormlyGrid:
                    retValue = CreateFormlyGrid();
                    break;
                case Layouts.Grid:
                    retValue = CreateGridDiv();
                    break;
                default:
                    break;
            }

            return retValue;
        }

        private string CreateEmpty()
        {
            string retValue = string.Empty;

            retValue = "<div class=\"panel-body\">\n\t\t\t\t<!-- PUT YOUR CONTENT HERE -->\n\t\t\t</div>\n";

            return retValue;
        }

        private string CreateTwoPanels()
        {
            string retValue = string.Empty;

            StringBuilder sb = new StringBuilder();
            sb.AppendLine("<div class=\"row\">");
            sb.AppendLine("\t\t\t\t<div class=\"col-lg-12\">");
            sb.AppendLine("\t\t\t\t\t<div class=\"panel-body\">\n\t\t\t\t\t\t<!-- PUT YOUR CONTENT HERE -->\n\t\t\t\t\t</div>");
            sb.AppendLine("\t\t\t\t</div>");
            sb.AppendLine("\t\t\t</div>");
            sb.AppendLine("\t\t\t<div class=\"row\">");
            sb.AppendLine("\t\t\t\t<div class=\"col-lg-12\">");
            sb.AppendLine("\t\t\t\t\t<div class=\"panel-body\">\n\t\t\t\t\t\t<!-- PUT YOUR CONTENT HERE -->\n\t\t\t\t\t</div>");
            sb.AppendLine("\t\t\t\t</div>");
            sb.AppendLine("\t\t\t</div>");

            retValue = sb.ToString();

            return retValue;
        }

        private string CreateFourPanels()
        {
            string retValue = string.Empty;

            StringBuilder sb = new StringBuilder();
            sb.AppendLine("<div class=\"row\">");
            sb.AppendLine("\t\t\t\t<div class=\"col-lg-6\">");
            sb.AppendLine("\t\t\t\t\t<div class=\"panel-body\">\n\t\t\t\t\t\t<!-- PUT YOUR CONTENT HERE -->\n\t\t\t\t\t\t</div>");
            sb.AppendLine("\t\t\t\t</div>");
            sb.AppendLine("\t\t\t\t<div class=\"col-lg-6\">");
            sb.AppendLine("\t\t\t\t\t<div class=\"panel-body\">\n\t\t\t\t\t\t<!-- PUT YOUR CONTENT HERE -->\n\t\t\t\t\t\t</div>");
            sb.AppendLine("\t\t\t\t</div>");
            sb.AppendLine("\t\t\t</div>");
            sb.AppendLine("\t\t\t\t<div class=\"row\">");
            sb.AppendLine("\t\t\t\t<div class=\"col-lg-6\">");
            sb.AppendLine("\t\t\t\t\t<div class=\"panel-body\">\n\t\t\t\t\t\t<!-- PUT YOUR CONTENT HERE -->\n\t\t\t\t\t\t</div>");
            sb.AppendLine("\t\t\t\t</div>");
            sb.AppendLine("\t\t\t\t<div class=\"col-lg-6\">");
            sb.AppendLine("\t\t\t\t\t<div class=\"panel-body\">\n\t\t\t\t\t\t<!-- PUT YOUR CONTENT HERE -->\n\t\t\t\t\t\t</div>");
            sb.AppendLine("\t\t\t\t</div>");
            sb.AppendLine("\t\t\t</div>");

            retValue = sb.ToString();

            return retValue;
        }

        private string CreateFormly()
        {
            string retValue = string.Empty;

            StringBuilder sb = new StringBuilder();
            sb.AppendLine("<div class=\"panel-body\">\n\t\t\t\t<!-- PUT YOUR CONTENT HERE -->");
            sb.AppendLine(CreateFormlyDiv());
            sb.AppendLine("\t\t\t</div>");

            retValue = sb.ToString();

            return retValue;
        }

        private string CreateFormlyTwoPanels()
        {
            string retValue = string.Empty;

            StringBuilder sb = new StringBuilder();
            sb.AppendLine("<div class=\"row\">");
            sb.AppendLine("\t\t\t\t<div class=\"col-lg-12\">");
            sb.AppendLine("\t\t\t\t\t<div class=\"panel-body\">\n\t\t\t\t\t\t<!-- PUT YOUR CONTENT HERE -->");
            sb.AppendLine(CreateFormlyDiv());
            sb.AppendLine("\t\t\t\t\t</div>");
            sb.AppendLine("\t\t\t\t</div>");
            sb.AppendLine("\t\t\t</div>");
            sb.AppendLine("\t\t\t<div class=\"row\">");
            sb.AppendLine("\t\t\t\t<div class=\"col-lg-12\">");
            sb.AppendLine("\t\t\t\t\t<div class=\"panel-body\">\n\t\t\t\t\t\t<!-- PUT YOUR CONTENT HERE -->\n\t\t\t\t\t</div>");
            sb.AppendLine("\t\t\t\t</div>");
            sb.AppendLine("\t\t\t</div>");

            retValue = sb.ToString();

            return retValue;
        }

        private string CreateFormlyFourPanels()
        {
            string retValue = string.Empty;

            StringBuilder sb = new StringBuilder();
            sb.AppendLine("<div class=\"row\">");
            sb.AppendLine("\t\t\t\t<div class=\"col-lg-6\">");
            sb.AppendLine("\t\t\t\t\t<div class=\"panel-body\">\n\t\t\t\t\t\t<!-- PUT YOUR CONTENT HERE -->");
            sb.AppendLine(CreateFormlyDiv());
            sb.AppendLine("\t\t\t\t\t</div>");
            sb.AppendLine("\t\t\t\t</div>");
            sb.AppendLine("\t\t\t\t<div class=\"col-lg-6\">");
            sb.AppendLine("\t\t\t\t\t<div class=\"panel-body\">\n\t\t\t\t\t\t<!-- PUT YOUR CONTENT HERE -->\n\t\t\t\t\t\t</div>");
            sb.AppendLine("\t\t\t\t</div>");
            sb.AppendLine("\t\t\t</div>");
            sb.AppendLine("\t\t\t<div class=\"row\">");
            sb.AppendLine("\t\t\t\t<div class=\"col-lg-6\">");
            sb.AppendLine("\t\t\t\t\t<div class=\"panel-body\">\n\t\t\t\t\t\t<!-- PUT YOUR CONTENT HERE -->\n\t\t\t\t\t\t</div>");
            sb.AppendLine("\t\t\t\t</div>");
            sb.AppendLine("\t\t\t\t<div class=\"col-lg-6\">");
            sb.AppendLine("\t\t\t\t\t<div class=\"panel-body\">\n\t\t\t\t\t\t<!-- PUT YOUR CONTENT HERE -->\n\t\t\t\t\t\t</div>");
            sb.AppendLine("\t\t\t\t</div>");
            sb.AppendLine("\t\t\t</div>");

            retValue = sb.ToString();

            return retValue;
        }

        private string CreateFormlyGrid()
        {
            string retValue = string.Empty;

            StringBuilder sb = new StringBuilder();
            sb.AppendLine("<div class=\"row\">");
            sb.AppendLine("\t\t\t\t<div class=\"col-lg-12\">");
            sb.AppendLine("\t\t\t\t\t<div class=\"panel-body\">\n\t\t\t\t\t\t<!-- PUT YOUR CONTENT HERE -->");
            sb.AppendLine(CreateFormlyDiv());
            sb.AppendLine("\t\t\t\t\t</div>");
            sb.AppendLine("\t\t\t\t</div>");
            sb.AppendLine("\t\t\t</div>");
            sb.AppendLine("\t\t\t<div class=\"row\">");
            sb.AppendLine("\t\t\t\t<div class=\"col-lg-12\">");
            sb.AppendLine("\t\t\t\t\t<div class=\"panel-body\">\n\t\t\t\t\t\t<!-- PUT YOUR CONTENT HERE -->");
            sb.AppendLine(CreateGridDiv());
            sb.AppendLine("\t\t\t\t\t</div>");
            sb.AppendLine("\t\t\t\t</div>");
            sb.AppendLine("\t\t\t</div>");

            retValue = sb.ToString();

            return retValue;
        }

        private string CreateFormlyDiv()
        {
            StringBuilder sb = new StringBuilder();

            sb.AppendLine("\t\t\t\t<div ng-app=\"formlyExample\" ng-controller=\"MainCtrl as vm\" name=\"divContainer\" id=\"divContainer\">");
            sb.AppendLine("\t\t\t\t\t<div ng-if=\"vm.loadingData.$$state.status === 0\" style=\"margin:20px 0;font-size:2em\">");
            sb.AppendLine("\t\t\t\t\t\t<strong>Loading...</strong>");
            sb.AppendLine("\t\t\t\t\t</div>");
            sb.AppendLine("\t\t\t\t\t<div ng-if=\"vm.loadingData.$$status.state !== 0\">");
            sb.AppendLine("\t\t\t\t\t\t<form ng-submit=\"vm.onSubmit()\" novalidate id=\"formlyForm\">");
            sb.AppendLine("\t\t\t\t\t\t\t<formly-form model=\"vm.model\" fields=\"vm.fields\" form=\"vm.form\">");
            sb.AppendLine("\t\t\t\t\t\t\t\t<button type=\"sumbit\" class=\"btn btn-primary submit-button\" ng-disabled=\"vm.form.$invalid\">Submit</button>");
            sb.AppendLine("\t\t\t\t\t\t\t</formly-form>");
            sb.AppendLine("\t\t\t\t\t\t</form>");
            sb.AppendLine("\t\t\t\t\t</div>");
            sb.AppendLine("\t\t\t\t</div>");

            return sb.ToString();
        }

        private string CreateGridDiv()
        {
            ControlLibraryDefinition = "<%@ Register assembly=\"GrayParrot.Web.Controls\" namespace=\"GrayParrot.Web.Controls\" tagprefix=\"cc1\" %>";

            StringBuilder sb = new StringBuilder();

            if (this.IsGridCRUD)
            {
                sb.AppendLine("\t\t\t\t<iframe id='iframeCrud' frameborder=\"0\" style=\"width:700px; height:600px; display:none\"></iframe>");
                sb.AppendLine("\t\t\t\t<div id=\"crudDialog\" title=\"\" style=\"display:none\"></div>");
                sb.AppendLine("\t\t\t\t<div id=\"deleteDiv\" title=\"\" style=\"display:none\"></div>");
                sb.Append("\n");
            }

            sb.AppendLine("\t<cc1:JQDataTable ID=\"JQDataTable1\" runat=\"server\"");
            sb.AppendLine("\t\t\t\t\tSelectionMode=\"Single\"");
            sb.AppendLine("\t\t\t\t\tRenderScripts=\"false\"");
            sb.AppendLine("\t\t\t\t\tBootstrapClasses=\"table table-hover table-bordered table-condensed\">");
            sb.AppendLine("\t\t\t\t\t<FeaturesConfig>");
            sb.AppendLine("\t\t\t\t\t\t<Features>");
            sb.AppendLine("\t\t\t\t\t\t\t<cc1:Searching Value=\"None\"></cc1:Searching>");
            sb.AppendLine("\t\t\t\t\t\t\t<cc1:AutoWitdh Value=\"true\" />");
            sb.AppendLine("\t\t\t\t\t\t\t<cc1:ScrollY Value=\"500\" ScrollCollapse=\"true\" />");
            sb.AppendLine("\t\t\t\t\t\t\t<cc1:LengthChange Value=\"true\" />");
            sb.AppendLine("\t\t\t\t\t\t\t<cc1:Paging PagingType=\"FullNumbers\" PageLength=\"20\" />");
            sb.AppendLine("\t\t\t\t\t\t</Features>");
            sb.AppendLine("\t\t\t\t\t</FeaturesConfig>");
            sb.AppendLine("\t\t\t\t\t<ControllerConfig Url=\"/api/<!-- URL OF CONTROLLER -->/Load\" LoadingMode=\"OnLoad\">");
            sb.AppendLine("\t\t\t\t\t</ControllerConfig>");
            sb.AppendLine("\t\t\t\t\t<ColumnsConfig Key=\"<!-- PUT KEY HERE -->\">");
            sb.AppendLine("\t\t\t\t\t\t<Columns>");
            sb.AppendLine("\t\t\t\t\t\t\t<!-- PUT HERE COLUMNS BASED ON CLASS PROPERTIES -->");
            sb.AppendLine("\t\t\t\t\t\t</Columns>");
            sb.AppendLine("\t\t\t\t\t</ColumnsConfig>");

            if (this.IsGridCRUD)
            {
                sb.AppendLine("\t\t\t\t\t<Buttons>");
                sb.AppendLine("\t\t\t\t\t\t<cc1:CrudAddButton Text=\"Aggiungi\" Callback=\"AddDialog\" />");
                sb.AppendLine("\t\t\t\t\t\t<cc1:CrudEditButton Text=\"Modifica\" Callback=\"EditDialog\" />");
                sb.AppendLine("\t\t\t\t\t\t<cc1:CrudDeleteButton Text=\"Cancella\" Callback=\"DeleteDialog\" />");
                sb.AppendLine("\t\t\t\t\t</Buttons>");


                GridCRUDActionCode += CreateAddDialog();
                GridCRUDActionCode += CreateEditDialog();
                GridCRUDActionCode += CreateDeleteDialog();

                GridDefinition = "<% HttpContext.Current.Response.Write(this.JQDataTable1.RenderScriptsManually()); %>";
            }

            sb.AppendLine("\t\t\t\t</cc1:JQDataTable>");

            return sb.ToString();
        }

        private string CreateAddDialog()
        {
            string retValue = string.Empty;

            StringBuilder sb = new StringBuilder();
            sb.AppendLine("function AddDialog() {");
            sb.AppendLine("\t\t\t$(\"#iframeCrud\").prop('src', '/* PUT HERE THE ADD METHOD URL */').appendTo(\"#crudDialog\");");
            sb.AppendLine("\t\t\t$(\"#iframeCrud\").show();");
            sb.AppendLine("\t\t\tm_dialog = $(\"#crudDialog\").dialog({");
            sb.AppendLine("\t\t\t\ttitle: 'DIALOG TITLE',");
            sb.AppendLine("\t\t\t\twidth: 'auto',");
            sb.AppendLine("\t\t\t\theight: 'auto',");
            sb.AppendLine("\t\t\t\tmodal: true,");
            sb.AppendLine("\t\t\t\tclose: function () {");
            sb.AppendLine("\t\t\t\t\tRedrawGrid(m_tableJQDataTable1);");
            sb.AppendLine("\t\t\t\t\t$(\"#iframeCrud\").contents().remove();");
            sb.AppendLine("\t\t\t\t\t$(\"#iframeCrud\").hide();");
            sb.AppendLine("\t\t\t\t}");
            sb.AppendLine("\t\t\t});");
            sb.AppendLine("\t\t};");

            retValue = sb.ToString();

            return retValue;
        }

        private string CreateEditDialog()
        {
            string retValue = string.Empty;

            StringBuilder sb = new StringBuilder();
            sb.AppendLine("function EditDialog() {");

            sb.AppendLine("var p = null;");
            sb.AppendLine("p = GetSelectedData(m_tableJQDataTable1).Id;");
            sb.AppendLine("var url = '/* PUT HERE THE EDIT METHOD URL */?id=' + p;");
            sb.AppendLine("$(\"#iframeCrud\").prop('src', url).appendTo(\"#crudDialog\");");
            sb.AppendLine("$(\"#iframeCrud\").show();");
            sb.AppendLine("m_dialog = $(\"#crudDialog\").dialog({");
            sb.AppendLine("\t\t\t\ttitle: 'DIALOG TITLE',");
            sb.AppendLine("\t\t\t\twidth: 'auto',");
            sb.AppendLine("\t\t\t\theight: 'auto',");
            sb.AppendLine("\t\t\t\tmodal: true,");
            sb.AppendLine("\t\t\t\tclose: function () {");
            sb.AppendLine("\t\t\t\t\tRedrawGrid(m_tableJQDataTable1);");
            sb.AppendLine("\t\t\t\t\t$(\"#iframeCrud\").contents().remove();");
            sb.AppendLine("\t\t\t\t\t$(\"#iframeCrud\").hide();");
            sb.AppendLine("\t\t\t\t}");
            sb.AppendLine("\t\t\t});");
            sb.AppendLine("\t\t};");

            retValue = sb.ToString();

            return retValue;
        }

        private string CreateDeleteDialog()
        {
            string retValue = string.Empty;

            StringBuilder sb = new StringBuilder();
            sb.AppendLine("function DeleteDialog() {");


            sb.AppendLine("var p = null;");
            sb.AppendLine("var row = GetSelectedData(m_tableJQDTGiocatori);");
            sb.AppendLine("p = row.Id;");
            sb.AppendLine("var url = '/api/AssociazioneGiocatori/Delete';");
            sb.AppendLine("var msg = 'SportRadar:[' + row.IdSportradar + '] - Sogei:[' + row.IdSogei + ']';");
            sb.AppendLine("$(\"#deleteDiv\").contents().remove();");
            sb.AppendLine("$(\"#deleteDiv\").html(\"Confermi la cancellazione dell'elemento con codice: \" + p + \"<br/>\" + msg + \" ?\").appendTo(\"#crudDialog\");");
            sb.AppendLine("$(\"#deleteDiv\").show();");
            sb.AppendLine("$(\"#crudDialog\").dialog({");
            sb.AppendLine("\t\t\t\tresizable: false,");
            sb.AppendLine("\t\t\t\tmodal: true,");
            sb.AppendLine("\t\t\t\ttitle: \"Cancella elemento\",");
            sb.AppendLine("\t\t\t\theight: 250,");
            sb.AppendLine("\t\t\t\twidth: 400,");
            sb.AppendLine("\t\t\t\tbuttons: {");
            sb.AppendLine("\t\t\t\t\t\"Yes\": function () {");
            sb.AppendLine("\t\t\t\t\t\t$(this).dialog('close');");
            sb.AppendLine("\t\t\t\t\t\t$.ajax({");
            sb.AppendLine("\t\t\t\t\t\t\ttype: \"POST\",");
            sb.AppendLine("\t\t\t\t\t\t\turl: url,");
            sb.AppendLine("\t\t\t\t\t\t\tdata: JSON.stringify({ id: p }),");
            sb.AppendLine("\t\t\t\t\t\t\tcontentType: \"application/json; charset=utf-8\",");
            sb.AppendLine("\t\t\t\t\t\t\tdataType: \"json\",");
            sb.AppendLine("\t\t\t\t\t\t\tsuccess: function (response) {");
            sb.AppendLine("\t\t\t\t\t\t\t\tRedrawGrid(m_tableJQDTGiocatori);");
            sb.AppendLine("\t\t\t\t\t\t\t\t$(\"#deleteDiv\").contents().remove();");
            sb.AppendLine("\t\t\t\t\t\t\t\t$(\"#deleteDiv\").hide();");
            sb.AppendLine("\t\t\t\t\t\t\t\talert(response);");
            sb.AppendLine("\t\t\t\t\t\t\t},");
            sb.AppendLine("\t\t\t\t\t\t\tfailure: function (response) {");
            sb.AppendLine("\t\t\t\t\t\t\t$(\"#deleteDiv\").contents().remove();");
            sb.AppendLine("\t\t\t\t\t\t\t$(\"#deleteDiv\").hide();");
            sb.AppendLine("\t\t\t\t\t\t\talert(response);");
            sb.AppendLine("\t\t\t\t\t\t}");
            sb.AppendLine("\t\t\t\t\t});");
            sb.AppendLine("\t\t\t\t},");
            sb.AppendLine("\t\t\t\t\t\"No\": function () {");
            sb.AppendLine("\t\t\t\t\t\t$(\"#deleteDiv\").contents().remove();");
            sb.AppendLine("\t\t\t\t\t\t$(\"#deleteDiv\").hide();");
            sb.AppendLine("\t\t\t\t\t\t$(this).dialog('close');");
            sb.AppendLine("\t\t\t\t\t}");
            sb.AppendLine("\t\t\t\t}");
            sb.AppendLine("\t\t\t});");

            sb.AppendLine("};");


            retValue = sb.ToString();

            return retValue;
        }
    }
}
